/*
* 标签库
* date 2013-1-9
*/
var tagDialog={
	setting:{
		ajaxUrl:'',
		preBtnId:'dialog_tag_pre',
		nextBtnId:'dialog_tag_next',
		contentId:'dialog_tag_content_value',
		transferId:'tagsHandle',
		currentPage:1
	},
	init:function(param){
		var that=this;
		that.setting=$.extend({},that.setting,param); 	
		that.addTemplateToDialog(that.template);
		that.initData();
		that.bindEvent();
	},
	addTemplateToDialog:function(html){
		linji_dialog.init({'title':'标签库','html':html});
		linji_dialog.position();
	},
	bindEvent:function(){
		var that=this;
		var $preBtn=$('#'+that.setting.preBtnId);
		var $nextBtn=$('#'+that.setting.nextBtnId);
		$preBtn.on('click',function(){
			that.setting.currentPage--;
		});
		$nextBtn.on('click',function(){
			that.setting.currentPage++;
		});	
		$('#'+that.setting.contentId).delegate("a", "click", function(){	
			var $this=$(this);
			var txt=$this.html();
			that.addTagToTransfer(txt);
		});
	},
	initData:function(){
		var that=this;
		var $content=$('#'+that.setting.contentId);
		var links=[];
		for(var i=0;i<50;i++)
		{
			links.push('<a href="javascript:void(0);" title="PHP">PHP'+i+'</a>');
		}
		$content.html(links.join(''));
	},
	addTagToTransfer:function(txt){
		var that=this,
		$transfer=$('#'+that.setting.transferId),
		currentTags=$transfer.val();
		if(currentTags=='')
		{
			currentTags=txt;
		}else{
					if(currentTags.indexOf(txt)==-1)
					{
						currentTags+=','+txt;
					}
				}
		$transfer.val(currentTags);		
	},
	template:'<div class="dialog_tag">'
            	 +'<div class="dialog_tag_content clearfix" id="dialog_tag_content_value">'
                 +' </div>'
                 +'<div class="clearfix">'
                	+'<input type="button" value="下一页" class="public_button fr"  id="dialog_tag_next"/>'
                    +'<input type="button" value="上一页" class="public_button fr mr10"  id="dialog_tag_pre"/>'
                +'</div>'
           +' </div>'
};

/**
* AJAX新增编辑标签
* @author 林吉
*/
var tagAddAndEdit={
	setting:{
		currentId:0,
		submitBtnId:'dialog_tag_submit'
	},	
	init:function(id){
		this.setting.currentId=id || this.setting.currentId;
		this.addTemplateToDialog(this.template);
		this.bindData();
		this.bindEvent();
		this.show();
	},
	addTemplateToDialog:function(html){
		var title='新增标签';
		if(this.setting.currentId!=0)
		{
			title='编辑标签';
		}
		linji_dialog.init({'title':title,'html':html,'width':'250'});
	},
	bindData:function(){
		var that=this;
		var $form=$('#ajaxTagForm');
		if(that.setting.currentId!=0)
		{
			$.ajax({
				type:'GET',
				dataType:'json',
				url:URL+'/edit/id/'+that.setting.currentId,
				success:function(msg){
					if(msg.status==1)
					{
						$form.find('#title').val(msg.data.title);
						$form.find('#tagId').val(msg.data.id);
						$form.attr('action',URL+'/edit');						
					}else
						{
							alert(msg.info);
							dialog.hide();
						}
				}
			});
		}else
			{
				$form.attr('action',URL+'/add');
			}
	},
	bindEvent:function(){
		var $form=$('#ajaxTagForm');
		$form.submit(function(){
			var $this=$(this);
			var value=$this.find('#title').val();
			if(value=='')
			{
				$this.find('#titleTip').html('请填写标题').addClass('error');
				return false;
			}
		});
		$form.find('#title').on('blur',function(){
			var $this=$(this);
			var value=$this.val();
			if(value=='')
			{
				$form.find('#titleTip').html('请填写标题').addClass('error');
				return false;
			}else
				 {
					$form.find('#titleTip').html('输入正确').addClass('success');
				 }			
		});
	},
	show:function(){
		linji_dialog.show();
	},		
	template:'<form method="post" id="ajaxTagForm">'
				 +' <div class="field"><label>标题</label><input class="text_input medium_input" type="text" name="title" id="title" value="" /><span class="input_notification  png_bg" id="titleTip"></span><small>标题不能为空</small></div>'
                 +'<div class="clearfix">'
				  +'<input type="hidden" value="" class="public_button fr"  id="tagId" name="id"/>'
                 +'<input type="submit" value="提交" class="public_button fr"  id="dialog_tag_submit"/>'
                 +'</div>'
                 +' </form>'		
};